const router = require('koa-router')()

const login = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../module/resModel')
const { genPassword } = require('../utils/cryp')

router.prefix('/api/user')
router.post('/login', async function (ctx, next) {
  const { username, password } = ctx.request.body
  const res = await login(username, genPassword(password))
  if (!res) {
    ctx.body = new ErrorModel('username or password is wrong!')
    return
  }
  ctx.session.username = username
  ctx.session.password = password
  ctx.body = new SuccessModel(res, 'login success 888')
})

module.exports = router
